<template>
    <div class="first">
        <div class="login_box">
            <!--logo区域-->
            <div class="login_logo">
                <img src="../assets/logo.png" alt="此图片为网站logo">
            </div>
            <!--登录表单区域-->
            <el-form ref="LoginFormRef" class="loginform" :model="LoginForm" :rules="LoginFormRules">
                <el-form-item prop="username">
                    <el-input placeholder="请输入用户名" v-model="LoginForm.username"
                        prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" v-model="LoginForm.password" show-password
                        prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item class="elbutn">
                    <el-button type="primary" @click="Login">登录</el-button>
                    <el-button type="info" @click="ResetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import axios from 'axios';
import request from "@/utils/request";


export default {
    data() {
        return {
        //这是登录表单的数据绑定对象 :model
            LoginForm: {
                username: 'admin',
                password: 'admin'
            },
        //这是登陆表单的数据验证规则对象  1.定义规则：:rules= ; 2.说明规则:(如下); 3.添加prop属性
            LoginFormRules: {
                username: [
                    { required: true, message: "请输入用户名", trigger: 'blur' },
                    { min: 2, max: 10, message: "长度在2-10个字符", trigger: 'blur' }
                ],
                password: [
                    { required: true, message: "请输入密码", trigger: 'blur' },
                ]
            }
        }
    },
    methods: {
        //重置整个表单输入域
        ResetLoginForm() {
            this.$refs.LoginFormRef.resetFields();
        },
        //处理登录事件
        Login() {
        //取到表单对象，this.$refs.LoginFormRef
        //再使用饿了么的validate方法进行表单验证。其中valide是表单验证结果true or false
            this.$refs.LoginFormRef.validate(valid => {
                if (!valid) return;
                request.post("user/login", this.LoginForm).then(res=>{
                    console.log("------------------------",res)
                    localStorage.setItem("token",res.token)
                })
                this.$router.push("/index/usermanage")
            });
        }
    }
}

</script>

<style lang="less" scoped>
.first {
    background-color: #2b4b6b;
    height: 100%;
}

.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.login_logo {
    width: 130px;
    height: 130px;
    padding: 10px;
    border-radius: 50%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    border: 1px solid #eee;
    box-shadow: 0 0 10px #ddd;

    img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
}

.loginform {
    padding: 20%;
}

.elbutn {
    display: flex;
    justify-content: space-between;
}
</style>